package com.ecarx.module_udisk.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.ecarx.module_udisk.R;
import com.ecarx.module_udisk.bean.PathBean;

import java.util.ArrayList;
import java.util.List;


public class PathAdapter extends RecyclerView.Adapter<PathAdapter.MyViewHolder> {
    private static final String TAG = "PathAdapter";
    private Context mContext;
    private List<PathBean> pathList;
    private recycleListStatusCallBack callBack;

    public PathAdapter(Context context) {
        this.mContext = context;
    }

    public void setPathList(List<PathBean> pathList) {
        this.pathList = pathList;
        notifyDataSetChanged();
    }

    public void setCallBack(recycleListStatusCallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_path, parent, false);
        MyViewHolder vh = new MyViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        PathBean pathBean = pathList.get(position);
        holder.tv.setText(pathBean.getShowPath());
        holder.cb.setOnCheckedChangeListener(null);
        holder.cb.setChecked(pathBean.isSelected());
        holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                pathBean.setSelected(b);
                checkAllBeanStatus();
                checkAnyPathCheck();
            }

            private void checkAnyPathCheck() {
                for (PathBean bean : pathList) {
                    if (bean.isSelected()) {
                        callBack.AnyPathCheckStatus(true);
                        return;
                    }
                }
                callBack.AnyPathCheckStatus(false);
            }

            private void checkAllBeanStatus() {
                for (PathBean bean : pathList) {
                    if (!bean.isSelected()) {
                        callBack.AllPathCheckStatus(false);
                        return;
                    }
                }
                callBack.AllPathCheckStatus(true);
            }
        });
    }

    //全部选择 or 全部不选择设置
    public void setAllPathCheckStatus(boolean b) {
        if(pathList == null){
            Log.d(TAG,"setAllPathCheckStatus pathList is null");
            return;
        }
        for (PathBean bean : pathList) {
            bean.setSelected(b);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (pathList == null || pathList.size() == 0) {
            return 0;
        }
        return pathList.size();
    }

    /**
     * 获取选中升级的文件路径
     *
     * @return
     */
    public List<String> getCheckPathList() {
        if(pathList == null){
            return null;
        }
        List<String> list = new ArrayList<>();
        for (PathBean bean : pathList) {
            if (bean.isSelected()) {
                list.add(bean.getPath());
            }
        }
        return list;
    }


    static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv;
        CheckBox cb;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.text);
            cb = itemView.findViewById(R.id.cb_check);
        }

    }

    public interface recycleListStatusCallBack {
        void AllPathCheckStatus(boolean b);
        void AnyPathCheckStatus(boolean b);
    }
}
